package array;

import java.util.Arrays;

/**
 * @Program: dataOJ->CircleArrayQueue
 * @Description: 循环队列
 * @Author: Tansty
 * @Date: 2022-01-16 19:09
 * @Version： 1.0
 **/

public class CircleArrayQueue {
    private int front;
    private int rear;
    private int maxSize;
    private int[] array;

    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        array = new int[maxSize];
        // 头指针指向第一个位置
        front = 0;
        // 尾指针指向最后一个+1的位置
        rear = 0;
    }

    public Boolean isEmpty() {
        return this.front == this.rear;
    }

    public Boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    public void addQueue(int n) {
        if (isFull()) {
            throw new RuntimeException("队列已经满");
        }
        this.array[rear] = n;
        // 每次填入队列，尾指针指向后一个位置
        rear = (rear + 1) % maxSize;
    }

    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        int head = this.array[front];
        front = (front + 1) % maxSize;
        return head;
    }

    public void showQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        for (int i = front; i < size(); i++) {
            System.out.printf("%s\t", array[i % maxSize]);
        }
        System.out.println();
    }

    public void showHead() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        System.out.println(array[front]);
    }

    public int size() {
        return (rear + maxSize - front) % maxSize;
    }

    public static void main(String[] args) {
        CircleArrayQueue queue = new CircleArrayQueue(5);
        queue.addQueue(5);
        queue.showHead();
        queue.showQueue();
        queue.addQueue(5);
        //System.out.println(queue.getQueue());
        queue.showQueue();
    }

}
